// JavaScript Document

OpenLayers.Control.ToolMeasure = 
  OpenLayers.Class(OpenLayers.Control, {

    /**
     * APIProperty: id
     * {String}
     */
	 id:null, 
    /**  
     * Property: displayContent
     * {FloatingDisplay}
     */
    displayContent: null,
	
	/**  
     * Property: measureControl
     * {OpenLayers.Control.Measure}
     */
   	 measureControl: null,
	 
	 /**  
     * Property: isActive
     * {boolean}
     */
   	 isActive: false,
		
	/** 
     * Size and initial positions for displayContent
     */
	displayTop: '10px',
	displayLeft: '60px',
	displayRight: '',
	displayWidth: '180px',
	displayHeight: '40px',
	 
    initialize: function(options) {
        OpenLayers.Control.prototype.initialize.apply(this, arguments);
        this.id = OpenLayers.Util.createUniqueID();
    },
	
	 	
	/** 
     * Method: ignoreEvent
     * 
     * Parameters:
     * evt - {Event} 
     */
    ignoreEvent: function(evt) {
        OpenLayers.Event.stop(evt);
    },
	
	/** 
     * Method: mouseDown
     *
     * Parameters:
     * evt - {Event}
     */
    mouseDown: function(evt) {
        this.isMouseDown = true;
        this.ignoreEvent(evt);
    },

    /** 
     * Method: mouseUp
     *  
     * Parameters:
     * evt - {Event} 
     */
    mouseUp: function(evt) {
        if (this.isMouseDown) {
            this.isMouseDown = false;
            this.ignoreEvent(evt);
        }
    },

	 /** 
     * Method: activate
     */
    activate: function() {
		var idAux = this.id;
		 if (this.measureControl==null){

         	this.measureControl = new OpenLayers.Control.Measure(
            	  OpenLayers.Handler.Path, {
                	 persist: true,
					 geodesic: true
            	    }
	              );
			
        	this.measureControl.events.on({
                    "measure": function(event){
		        		var geometry = event.geometry;
		                var units = event.units;
		                var measure = event.measure;
		        		var element = document.getElementById("floatingDisplay-containerMeasure"+idAux+"");
		                out = "Distancia: " + measure.toFixed(3) + " " + units;
		        		element.innerHTML = out;
		        	},
                    "measurepartial":function(event){
		        		var geometry = event.geometry;
		                var units = event.units;
		                var measure = event.measure;
		        		var element = document.getElementById("floatingDisplay-containerMeasure"+idAux+"");
		                out = "Distancia: " + measure.toFixed(3) + " " + units;
		        		element.innerHTML = out;
		        	}
            }); 
						
	     	this.map.addControl(this.measureControl);
		 }	
		 this.measureControl.activate();
		 
		 
		 if (this.displayContent==null){
			var meassure = this.measureControl;
			var onclosefunction = function(){meassure.cancel(); meassure.deactivate(); this.isActive=false;};
			this.displayContent = new OpenLayers.Control.FloatingDisplay({title:'Distancias', 
																  onClose:onclosefunction, 
																  top:this.displayTop,
																  left:this.displayLeft,
																  right:this.displayRight,
																  width:this.displayWidth,
																  height:this.displayHeight});
			this.map.addControl(this.displayContent);
		 }

		 var content = '<div id="floatingDisplay-containerMeasure'+this.id+'"></div>';
		 this.displayContent.addContent(content);
  		 this.displayContent.show();		 
		 this.isActive=true;
	},
	
	deactivate: function(){
	  	this.displayContent.hide();
  	},
	/*
	handleMeasurements: function(event){
		var geometry = event.geometry;
        var units = event.units;
        var measure = event.measure;
        alert("div: floatingDisplay-containerMeasure"+jjj+"");
		var element = document.getElementById("floatingDisplay-containerMeasure"+this.id+"");
        out = "Distancia: " + measure.toFixed(3) + " " + units;
		element.innerHTML = out;
	},*/
	
	CLASS_NAME: "OpenLayers.Control.ToolMeasure"

});